package cn.bytecollege.common.entity;

import java.util.List;

public class Page<T> {
    /**
     * 页面显示的数据条数
     */
    private int pageSize = 10;
    /**
     * 当前页码
     */
    private int currentIndex;
    /**
     *总条数
     */
    private int total;
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 页面上显示的页码
     */
    private String[] pageIndex;

    private List<T> list;
    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getTotalPage() {
        return totalPage;
    }

    /**
     * @param total total指总的条数，传入总条数后，可以计算出总的页数
     */
    public void setTotalPage(int total) {
        totalPage = (total+pageSize-1)/pageSize;
    }

    public String[] getPageIndex() {
        return pageIndex;
    }
    public void  setPageIndex(){
        pageIndex = new String[10];
        //共显示10个页码
        //判断总页数是否大于10
        if(totalPage<=pageIndex.length){
            for (int i = 1; i <=totalPage ; i++) {
                pageIndex[i-1] = String.valueOf(i);
            }
            return;
        }
        //代码执行到这里说明总页数大于10
        if(currentIndex<8){
            for (int i = 1; i <=pageIndex.length-2 ; i++) {
                pageIndex[i-1] = String.valueOf(i);
            }
            pageIndex[pageIndex.length-2] = "...";
            pageIndex[pageIndex.length-1] = String.valueOf(totalPage);
        }else if(currentIndex>=8&&totalPage-currentIndex>=7){
            pageIndex[0] = "...";
            pageIndex[pageIndex.length-1] = "...";
            int index = currentIndex-4;
            for (int i=1;i<=pageIndex.length-2;i++){
                pageIndex[i] = String.valueOf(index);
                index++;
            }
        }else if(totalPage-currentIndex<=8){
            int index = totalPage;
            for (int i = pageIndex.length-1;i>1;i--){
                pageIndex[i] = String.valueOf(index);
                index--;
            }
            pageIndex[0] = String.valueOf(1);
            pageIndex[1] = "...";
        }
    }
    public List<T> getList() {
        return list;
    }
    public void setList(List<T> list) {
        this.list = list;
    }
}
